//
//  ZLSafetyPswView.m
//  FinanceSupermarket
//
//  Created by xue on 2018/12/20.
//  Copyright © 2018年 xuezi. All rights reserved.
//

#import "ZLSafetyPswView.h"
#define kDotSize CGSizeMake (14, 14) // 密码点的大小
#define kDotCount 6  // 密码个数
#define K_Field_Height self.frame.size.height  // 每一个输入框的高度等于当前view的高度
@interface ZLSafetyPswView()<UITextFieldDelegate>
// 密码输入文本框
@property (nonatomic, strong) UITextField *pswTextField;
// 用于存放加密黑色点
@property (nonatomic, strong) NSMutableArray *dotArr;
@end

@implementation ZLSafetyPswView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor =[UIColor clearColor];



        [self setupWithPswTextField];
    }
    return self;
}

#pragma mark - 懒加载
- (NSMutableArray *)dotArr {
    if (!_dotArr) {
        _dotArr = [NSMutableArray array];
    }
    return _dotArr;
}

- (void)setupWithPswTextField {
    
    // 每个密码输入框的宽度
    CGFloat width = self.frame.size.width / kDotCount;
    
    // 生成分割线
    for (int i = 0; i < kDotCount - 1; i++) {
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.pswTextField.frame) + (i + 1) * width, 0, 1, K_Field_Height)];
        lineView.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:lineView];
    }
    
    self.dotArr = [[NSMutableArray alloc] init];
    
    // 生成中间的黑点
    for (int i = 0; i < kDotCount; i++) {
        UIView *dotView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.pswTextField.frame) + (width - kDotCount) / 2 + i * width, CGRectGetMinY(self.pswTextField.frame) + (K_Field_Height - kDotSize.height) / 2, kDotSize.width, kDotSize.height)];
        dotView.backgroundColor = [UIColor blackColor];
        dotView.layer.cornerRadius = kDotSize.width / 2.0f;
        dotView.clipsToBounds = YES;
        dotView.hidden = YES; // 首先隐藏
        [self addSubview:dotView];
        
        // 把创建的黑色点加入到存放数组中
        [self.dotArr addObject:dotView];
    }
}

//创建一个UITextField.切记输入的文字颜色和输入框光标的颜色为透明!
#pragma mark - init
- (UITextField *)pswTextField {
    
    if (!_pswTextField) {
        _pswTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, K_Field_Height)];
        _pswTextField.backgroundColor = [UIColor whiteColor];
        // 输入的文字颜色为无色
        _pswTextField.textColor = [UIColor clearColor];
        // 输入框光标的颜色为无色
        _pswTextField.tintColor = [UIColor clearColor];
        _pswTextField.delegate = self;
        _pswTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _pswTextField.keyboardType = UIKeyboardTypeNumberPad;
        _pswTextField.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        _pswTextField.layer.borderWidth = 1;
        _pswTextField.layer.cornerRadius =4;
        _pswTextField.layer.masksToBounds =YES;
        [_pswTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [self addSubview:_pswTextField];
    }
    return _pswTextField;
}


//文本框内容改变时,用来用户输入, 监听其值的改变.
#pragma mark - 文本框内容改变
/**
 *  重置显示的点
 */
- (void)textFieldDidChange:(UITextField *)textField {
    
    NSLog(@"目前输入显示----%@", textField.text);
    
    for (UIView *dotView in self.dotArr) {
        dotView.hidden = YES;
    }
    for (int i = 0; i < textField.text.length; i++) {
        ((UIView *)[self.dotArr objectAtIndex:i]).hidden = NO;
    }
    if (textField.text.length == kDotCount) {
        NSLog(@"---输入完毕---");
        
        [self.pswTextField resignFirstResponder];
    }
    
    // 获取用户输入密码
    !self.passwordDidChangeBlock ? : self.passwordDidChangeBlock(textField.text);
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSLog(@"输入变化%@", string);
    if([string isEqualToString:@"\n"]) { // 按回车关闭键盘
        
        [textField resignFirstResponder];
        return NO;
    } else if(string.length == 0) { // 判断是不是删除键
        
        return YES;
    } else if(textField.text.length >= kDotCount) { // 输入的字符个数大于6，则无法继续输入，返回NO表示禁止输入
        
        NSLog(@"输入的字符个数大于6，后面禁止输入则忽略输入");
        return NO;
    } else {
        
        return YES;
    }
}

//清除密码时收起键盘并将文本输入框值置为空.
#pragma mark - publick method
/**
 *  清除密码
 */
- (void)clearUpPassword {
    [self.pswTextField resignFirstResponder];
    self.pswTextField.text = nil;
    [self textFieldDidChange:self.pswTextField];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
